package stu.aistar.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import stu.aistar.model.pojo.Ciname;
import stu.aistar.model.pojo.Hall;
import stu.aistar.model.pojo.Movie;
import stu.aistar.service.ICinameService;
import stu.aistar.service.IHallService;
import stu.aistar.service.IMovieService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cinames")
public class CinameController {

    @Autowired
    private ICinameService cinameService;

    @Autowired
    private IHallService hallService;

    @Autowired
    private IMovieService movieService;

    @RequestMapping("/findAllAddress")
    public List<String> findAllAddress(HttpServletRequest request){
        String name = request.getParameter("term");
        List<String> address = cinameService.findAllAddress(name);
        return address;
    }

    @RequestMapping("/findByAddress")
    public Map<String,Object> findByAddress(@RequestParam("address") String address,Model model){
        Ciname ciname = cinameService.findByAddress(address);
        List<Hall> halls = hallService.findByCid(ciname.getId());
        List<Movie> movies = movieService.findAll();
        List<Map<String,Object>> hall_list = new ArrayList<>();
        List<Map<String,Object>> movie_list = new ArrayList<>();
        Map<String,Object> map =new HashMap<>();
        Map<String,Object> map1 = null;
        for (Hall hall:halls){
            map1 = new HashMap<>();
            map1.put("title",hall.getType());
            map1.put("id",hall.getId());
            hall_list.add(map1);
        }
        map.put("halls",hall_list);
        System.out.println(map);
        if(map1!=null){
            map1 = null;
        }
        for (Movie movie:movies){
            map1= new HashMap<>();
            map1.put("title",movie.getName());
            map1.put("id",movie.getId());
            movie_list.add(map1);
        }
        map.put("ciname",ciname);
        map.put("movies",movie_list);
        return map;
    }
    //@RequestMapping("/findAll")
    //public Map<String,Object> findAll(HttpServletRequest request){
    //    String page = request.getParameter("page");
    //    String pageSize = request.getParameter("limit");
    //    String params = request.getParameter("searchParams");
    //    String name=null;
    //    //先转换成JSONObject类型
    //    if (null != params){
    //        JSONObject obj = JSONObject.parseObject(params);
    //        name= obj.getString("name");
    //    }
    //    Map<String,Object> map = new HashMap<>();
    //    PageInfo<Ciname> pageInfo = cinameService.findByPage(page, pageSize,name);
    //    map.put("code",0);
    //    map.put("msg","查找成功");
    //    //数据总数
    //    map.put("count",pageInfo.getTotal());
    //    map.put("data",pageInfo.getList());
    //    return map;
    //}
    //
    //
    //@PostMapping("/save")
    //public Result saveCiname(Ciname ciname){
    //    Result result;
    //    Integer row = cinameService.saveMovie(ciname);
    //    if(row == 1){
    //        result = new Result("200","添加成功",null);
    //    }else {
    //        result = new Result("500","添加失败",null);
    //    }
    //    return result;
    //}
    //
    //@PostMapping("/deleteById")
    //public Result deleteCiname(@RequestParam("id") Integer id){
    //    Result result;
    //    Integer row = cinameService.deleteCinameById(id);
    //    if(row == 1){
    //        result = new Result("200","删除成功",null);
    //    }else {
    //        result = new Result("500","删除失败",null);
    //    }
    //    return result;
    //}
    //
    //
    //@PostMapping("/editCiname")
    //public Result editCiname(Ciname ciname){
    //    Result result;
    //    Integer row = cinameService.editCiname(ciname);
    //    if(row== 1){
    //        result = new Result("200","修改成功",null);
    //    }else {
    //        result = new Result("500","查找失败",null);
    //    }
    //    return result;
    //}
}
